
require("MuMIn")
require("xtable")
require("stargazer")
require("ggplot2")

load("replication_data.rda")


############################
#                          # 
# Replication for Table 1  #
#                          #  
############################


model1 <- glm(turnout_10 ~ l_stops_10 + black_10 + latino_10  + noncitizen_10 + samehouse_10 + 
                   homeowner_10 + l_population_10 + l_median_hh_inc_10 + single_mother_10 + scale_08,
              data = data[data$POP100 > 100, ], family = quasibinomial, weights = VEP10)

model2 <- glm(turnout_10 ~ l_search_frisks_10 + black_10 + latino_10  + noncitizen_10 +  samehouse_10 + 
                   homeowner_10 + l_population_10 + l_median_hh_inc_10 + single_mother_10 + scale_08,
              data = data[data$POP100 > 100, ], family = quasibinomial, weights = VEP10)

model3 <- glm(turnout_10 ~ l_force_10 + black_10 + latino_10 + noncitizen_10 +  samehouse_10 + 
                   homeowner_10 + l_population_10 + l_median_hh_inc_10 + single_mother_10 + scale_08,
              data = data[data$POP100 > 100, ], family = quasibinomial, weights = VEP10)

model4 <- glm(turnout_10 ~ l_furtive_10 + black_10 + latino_10  +  noncitizen_10 + samehouse_10 + 
                   homeowner_10 + l_population_10 + l_median_hh_inc_10 + single_mother_10 + scale_08,
              data = data[data$POP100 > 100, ], family = quasibinomial, weights = VEP10)

labels <- c("(Intercept)", "Log(Stops)", "Black", "Latino", "Non-Citizen", "Same House", "Homeowner", 
           "Log(Population)", "Log(Income)", "Single Mother", "Turnout 08")

New_York_2010_Models <- list(model1, model2, model3, model4)
New_York_2010_Models <- lapply(New_York_2010_Models, change.names, labels)

New_York_2010_CI = list(exp(confint(model1)), exp(confint(model2)), exp(confint(model3)), exp(confint(model4)))

McF_R2 = list( c("Pseudo R$^{2}$", round(unlist(lapply(New_York_2010_Models, pseudo.R.quasi)),2)))

stargazer(New_York_2010_Models, type="latex", title = "Effect of Police Stops on Voter Turnout: 2010", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, intercept.bottom = T,
          font.size="small", apply.coef=exp, ci.custom= New_York_2010_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, #covariate.labels=labels,
          column.labels= c("\\textbf{Stops}", "\\textbf{Frisks and Searches}", "\\textbf{Force}", 
                           "\\textbf{Furtive Movements}"))


############################
#                          # 
# Replication for Table 2  #
#                          #  
############################


model5 <- glm( turnout_08 ~ l_stops_08 + black_08 + latino_08 + noncitizen_08 + samehouse_08 + 
                   homeowner_08 + l_population_08 + l_median_hh_inc_08 + single_mother_08 + scale_06,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP08)

model6 <- glm( turnout_08 ~ l_search_frisks_08  + black_08 + latino_08 +  noncitizen_08 + samehouse_08 + 
                   homeowner_08 + l_population_08 + l_median_hh_inc_08 + single_mother_08 + scale_06,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP08)

model7 <- glm( turnout_08 ~ l_force_08 + black_08 + latino_08 + noncitizen_08 + samehouse_08 + 
                   homeowner_08 + l_population_08 + l_median_hh_inc_08 + single_mother_08 + scale_06,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP08)
                  
model8 <- glm( turnout_08 ~ l_furtive_08 + black_08 + latino_08 + noncitizen_08 + samehouse_08 + 
                   homeowner_08 + l_population_08 + l_median_hh_inc_08 + single_mother_08 + turnout06,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP08)

labels <- c("(Intercept)", "Log(Stops)", "Black", "Latino", "Non-citizen", "Same House", "Homeowner", 
           "Log(Population)", "Log(Income)", "Single Mother", "Turnout 06")

New_York_2008_Models <- list(model5, model6, model7, model8)
New_York_2008_Models <- lapply(New_York_2008_Models, change.names, labels)

New_York_2008_CI = list(exp(confint(model5)), exp(confint(model6)), exp(confint(model7)), exp(confint(model8)))


McF_R2 = list( c("Pseudo R$^{2}$", round(unlist(lapply(New_York_2008_Models, pseudo.R.quasi)),2)))


stargazer(New_York_2008_Models, type="latex", title = "Effect of Police Stops on Voter Turnout: 2008", style ="apsr", 
                    dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
                    font.size="small", apply.coef=exp, ci.custom= New_York_2008_CI, ci = T, p.auto=F,
                    star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, #covariate.labels=labels,
                    column.labels= c("\\textbf{Stops}", "\\textbf{Frisks and Searches}", "\\textbf{Force}", "\\textbf{Furtive Movements}"))



############################
#                          # 
# Replication for Table 3  #
#                          #  
############################

model9 <- glm(turnout_06 ~ l_stops_06 + black_06 + latino_06 + noncitizen_06
                  + samehouse_06 + homeowner_06 + l_population_06 + l_median_hh_inc_06 + single_mother_06,
                  data=data[data$POP100 > 100,], family = quasibinomial, weights = VEP06)

model10 <- glm(turnout_06 ~ l_search_frisks_06 + black_06+ latino_06 +  noncitizen_06
                  + samehouse_06 + homeowner_06 + l_population_06 + l_median_hh_inc_06 + single_mother_06,
                  data=data[data$POP100 > 100,], family = quasibinomial, weights = VEP06)

model11 <- glm(turnout_06 ~ l_force_06 +  black_06 + latino_06 + noncitizen_06
                  + samehouse_06 + homeowner_06 + l_population_06 + l_median_hh_inc_06 + single_mother_06,
                  data = data[data$POP100 > 100, ], family = quasibinomial, weights = VEP06)

model12 <- glm(turnout_06 ~ l_furtive_06  +  black_06 + latino_06 + noncitizen_06
                  + samehouse_06 + homeowner_06 + l_population_06 +  l_median_hh_inc_06 + single_mother_06,
                  data = data[data$POP100 > 100, ], family = quasibinomial, weights = VEP06) 


labels <- c("(Intercept)", "Log(Stops)",  "Black", "Latino", "Non-citizen", "Same House", "Homeowner", 
           "Log(Population)", "Log(Income)", "Single Mother")

New_York_2006_Models <- list(model9, model10, model11, model12)
New_York_2006_Models <- lapply(New_York_2006_Models, change.names, labels)

New_York_2006_CI = list(exp(confint(model9)), exp(confint(model10)), exp(confint(model11)), exp(confint(model12)))

McF_R2 = list( c("Pseudo R$^{2}$", round(unlist(lapply(New_York_2006_Models, pseudo.R.quasi)),2)))


stargazer(New_York_2006_Models, type="latex", title = "Effect of Police Stops on Voter Turnout: 2006", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= New_York_2006_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, #covariate.labels=labels,
          column.labels= c("\\textbf{Stops}", "\\textbf{Frisks and Searches}", "\\textbf{Force}", "\\textbf{Furtive}"))



############################
#                          # 
# Replication for Table 4  #
#                          #  
############################


primary <- glm(turnout_primary ~ l_stops + black + latino + samehouse + homeowner + 
                            l_population + l_median_hh_inc + single_mother + share_dem + turnout_dmp09,
               data = data13[data13$pop_10 > 100, ],  family= quasibinomial, weights = VEP13)

mayoral.general <- glm(turnout_general ~ l_stops + black + latino + samehouse + homeowner + 
                            l_population + l_median_hh_inc + single_mother + share_dem + turnout_mg09, 
                       data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = VEP13)

mayoral_models = list(primary, mayoral.general)

mayoral_models_CI = list(exp(confint(primary)), exp(confint(mayoral.general)))

McF_R2 = list( c("Pseudo R$^{2}$", round(unlist(lapply(mayoral_models, pseudo.R.quasi)),2)))

labels <- c("(Intercept)", "Log(Stops)", "Black", "Latino", "Same House", "Homeowner", "Log(Population)", "Log(Income)", "Single Mother", 
            "Registered Democrats", "Turnout 09")

mayoral_models <- lapply(mayoral_models, change.names, labels)


# The version of this table in the main manuscript was formatted slightly with separate labels for primary/general turnout 09

stargazer(mayoral_models,  type="latex", title = "Effect of Police Stops on Voter Turnout: 2013 Democratic Primary and General Elections", 
          style ="apsr",dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= mayoral_models_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, #covariate.labels= labels,
          column.labels= c("\\textbf{Primary  Election}", "\\textbf{Mayoral Election}"))


############################
#                          # 
# Replication for Table 5  #
#                          #  
############################




thompson <- glm(thompson13 ~ l_stops + black + latino + samehouse + homeowner +
                l_population + l_median_hh_inc  + single_mother + turnout_dmp09 + thompson_09, 
                data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = vote_13dmp)

deblasio <- glm(deblasio13 ~ l_stops + black + latino + samehouse + homeowner +
                     l_population  + l_median_hh_inc + single_mother + turnout_dmp09 + deblasio_09,
                data = data13[data13$pop_10 > 100, ], family= quasibinomial,  weights = vote_13dmp)

liu <- glm(liu13 ~ l_stops +  black + latino + samehouse + homeowner +
                l_population + l_median_hh_inc+ single_mother + turnout_dmp09 + liu_09,
           data = data13[data13$pop_10 > 100, ], family= quasibinomial,  weights = vote_13dmp)

candidate_suport <- list(thompson, deblasio, liu)

candidate_suport_CI = list(exp(confint(thompson)), exp(confint(deblasio)), exp(confint(liu)))

McF_R2 = list( c("Pseudo $R^{2}$", round(unlist(lapply(candidate_suport, pseudo.R.quasi)),2)))

labels <- c("Log(Stops)", "Black", "Latino", "Same House", "Homeowner", "Log(Population)", "Log(Income)", "Single Mother",
            "Turnout 09: Primary", "Thompson '09", "De Blasio '09", "Liu '09", "Constant")



stargazer(candidate_suport, type="latex", title = "Effect of Police Stops on Voter Turnout: 2013 Municipal Elections", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= candidate_suport_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, covariate.labels = labels,
          column.labels= c("\\textbf{Thompson}", "\\textbf{de Blasio}", "\\textbf{Liu}"))




############################
#                          # 
# Replication for Table 6  #
#                          #  
############################


# Table 6 summarizes Table [###] through Table [###] in presented in the Online Appendix


############################
#                          # 
# Replication for Table 7  #
#                          #  
############################


# Table 7 simply summarizes Table [###] through Table [###] in presented in the Online Appendix


############################
#                          # 
# Replication for Figure 1 #
#                          #  
############################

tiff('fig1.tiff', width = 6.85039, height =3.853344375, units= "in", res=600)


ggplot(plot_frame, aes(year, est, group = Treatment)) + 
  geom_col(aes(fill = Treatment),  position = "dodge") +
     scale_fill_grey() +
     scale_y_continuous("Estimated Treatment Effect (% Turnout)", labels = scales::percent, limits = c(-.0125, NA)) +
     xlab("Election") +
     geom_hline(yintercept = 0) +
     geom_text( aes(label = text, y = est + .001*sign(est)),  position = position_dodge(.9)) +
     theme(panel.grid.major = element_line(colour="#f0f0f0"),
           panel.grid.minor = element_blank(),
           panel.background = element_blank(),
           plot.subtitle = element_text(hjust = 0.5),
           plot.title = element_text(hjust = 0.5),
           axis.text=element_text(size=11))

dev.off()

